
package com.benimetum.managers;

import com.benimetum.database.Sparql;
import com.benimetum.items.Community;
import com.benimetum.items.Person;
import com.benimetum.items.Student;
import com.benimetum.items.Teacher;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author Ethem Cem Ozkan
 * @email ethemcem.ozkan@gmail.com
 * No 07110604
 */
public class SearchManager
{
    
    /**
     * hem ada hem soyada göre arama yapar
     * @param text
     * @return 
     */
    public static Student[] searchStudent(String text){
	  String query="select ?email ?id ?firstName ?lastName ?status ?picture ?mentor where {?student rdf:type etu:Student."
		    + "?student etu:email ?email. ?student etu:id ?id. ?student foaf:firstName ?firstName. ?student etu:status ?status. ?student foaf:surname ?lastName. "
		    + "?student etu:profilePicture ?picture. ?student etu:hasMentor ?teacher. ?teacher etu:id ?mentor. FILTER regex(?firstName, \""+text+"\", \"i\" ) }";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  Iterator<QuerySolution> iterator = toList.iterator();
	  ArrayList<Student> students=new ArrayList<Student>();
	  while(iterator.hasNext()){
		QuerySolution next = iterator.next();
		Teacher teacher = TeacherManager.getTeacherById(next.get("mentor").toString());
		students.add(new Student(next.get("email").toString(), next.get("id").toString(), next.get("firstName").toString(), next.get("lastName").toString(), next.get("status").toString(), next.get("picture").toString(), teacher));
	  }
	  query="select ?email ?id ?firstName ?lastName ?status ?picture ?mentor where {?student rdf:type etu:Student."
		    + "?student etu:email ?email. ?student etu:id ?id. ?student foaf:firstName ?firstName. ?student etu:status ?status. ?student foaf:surname ?lastName. "
		    + "?student etu:profilePicture ?picture. ?student etu:hasMentor ?teacher. ?teacher etu:id ?mentor. FILTER regex(?surname, \""+text+"\", \"i\" ) }";
	  search = Sparql.search(query);
	  toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  iterator = toList.iterator();
	  while(iterator.hasNext()){
		QuerySolution next = iterator.next();
		Teacher teacher = TeacherManager.getTeacherById(next.get("mentor").toString());
		students.add(new Student(next.get("email").toString(), next.get("id").toString(), next.get("firstName").toString(), next.get("lastName").toString(), next.get("status").toString(), next.get("picture").toString(), teacher));
	  }
	  Student[] array=new Student[students.size()];
	  return students.toArray(array);
    }
    
    /**
     *hem ada hem soyada göre öğretmen arar 
     * @param text
     * @return 
     */
    public static Teacher[] searchTeacher(String text){
	  String query="select ?id ?email ?firstName ?surname ?status ?picture "
		    + "where {?teacher rdf:type etu:Teacher. ?teacher etu:id ?id. ?teacher etu:email ?email."
		    + "?teacher foaf:firstName ?firstName. ?teacher foaf:surname ?surname. ?teacher etu:status ?status."
		    + "?teacher etu:profilePicture ?picture.FILTER regex(?firstName, \""+text+"\", \"i\" )}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  Iterator<QuerySolution> iterator = toList.iterator();
	  ArrayList<Teacher> teachers=new ArrayList<Teacher>();
	  while(iterator.hasNext()){
		QuerySolution next = iterator.next();
		teachers.add(new Teacher(next.get("email").toString(), next.get("id").toString(), next.get("firstName").toString(), next.get("surname").toString(), next.get("status").toString(), next.get("picture").toString()));
	  }
	  query="select ?id ?email ?firstName ?surname ?status ?picture "
		    + "where {?teacher rdf:type etu:Teacher. ?teacher etu:id ?id. ?teacher etu:email ?email."
		    + "?teacher foaf:firstName ?firstName. ?teacher foaf:surname ?surname. ?teacher etu:status ?status."
		    + "?teacher etu:profilePicture ?picture.FILTER regex(?surname, \""+text+"\", \"i\" )}";
	  search = Sparql.search(query);
	  toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  iterator = toList.iterator();
	  while(iterator.hasNext()){
		QuerySolution next = iterator.next();
		teachers.add(new Teacher(next.get("email").toString(), next.get("id").toString(), next.get("firstName").toString(), next.get("surname").toString(), next.get("status").toString(), next.get("picture").toString()));
	  }
	  Teacher[] array=new Teacher[teachers.size()];
	  return teachers.toArray(array);
    }
    
    public static Community[] searchCommunity(String text){
	  String query="select ?id ?name ?adminId where{?comm rdf:type etu:Community.?comm etu:name ?name. ?comm etu:id ?id. ?comm etu:createdBy ?admin. ?admin etu:id ?adminId. FILTER regex(?name, \""+text+"\", \"i\" )}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  ArrayList<Community> groups=new ArrayList<Community>();
        Iterator<QuerySolution> iterator = toList.iterator();
	  while(iterator.hasNext()){
            QuerySolution next = iterator.next();
		String adminId=next.get("adminId").toString();
		Person admin = null;
		if(adminId.startsWith("t"))
		    admin=TeacherManager.getTeacherById(adminId);
		else if(adminId.startsWith("st"))
		    admin=StudentManager.findStudentById(adminId);
		groups.add(new Community(next.get("id").toString(), next.get("name").toString(), CommunityManager.getCommunityMembers(next.get("id").toString()), admin));
	  }
	  Community[] array=new Community[groups.size()];
	  return groups.toArray(array);
    
    }
}
